######################################
# Generate plots conjoint data
######################################

rm(list=ls())

library("ggplot2")
library(gridExtra)
library(ggpubr)
library(grid)

# set working directory
setwd("~/Dropbox/Cerrillos 2017/08_replication")

####################
# left no likely1
####################

# load data
d <- read.table("016_left_nolikely1_alternative.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"

  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  

  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_left_nolikely1 = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Unlikely voters") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_left_nolikely1 

####################
# left likely1
####################

# load data
d <- read.table("017_left_likely1_alternative.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_left_likely1 = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Likely voters") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_left_likely1 

####################
# left difference
####################

# load data
d <- read.table("018_left_likely1_difference_alternative.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_left_likely1_difference = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Difference") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_left_likely1_difference

####################
# right no likely1
####################

# load data
d <- read.table("019_right_nolikely1_alternative.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_right_nolikely1 = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Unlikely voters") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_right_nolikely1 

####################
# right likely1
####################

# load data
d <- read.table("020_right_likely1_alternative.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_right_likely1 = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Likely voters") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_right_likely1 

####################
# right difference
####################

# load data
d <- read.table("021_right_likely1_difference_alternative.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_right_likely1_difference = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Difference") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_right_likely1_difference

####################
# center no likely1
####################

# load data
d <- read.table("022_center_nolikely1_alternative.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_center_nolikely1 = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Unlikely voters") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_center_nolikely1 

####################
# center likely1
####################

# load data
d <- read.table("023_center_likely1_alternative.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_center_likely1 = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Likely voters") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_center_likely1 

####################
# center difference
####################

# load data
d <- read.table("024_center_likely1_difference_alternative.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_center_likely1_difference = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Difference") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_center_likely1_difference

#########################
# no ideological no likely1
#########################

# load data
d <- read.table("025_noideo_nolikely1_alternative.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_noideo_nolikely1 = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Unlikely voters") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_noideo_nolikely1 

#######################
# non ideological likely1
#######################

# load data
d <- read.table("026_noideo_likely1_alternative.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_noideo_likely1 = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Likely voters") + theme(plot.title = element_text(size=25, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_noideo_likely1 

##############################
# non ideological difference
##############################

# load data
d <- read.table("027_noideo_likely1_difference_alternative.txt",comment.char="")

# group vars
Ideology     <- paste(c("1b",2),".atideology",sep="")
Profession     <- paste(c("1b",2,3),".atprofession",sep="")
Age   <- paste(c("1b",2,3),".atage",sep="")

CIs <- function(d){
  colnames(d)[1:2] <- c("pe","se")
  d$upper <-d$pe + 1.96*d$se
  d$lower <-d$pe - 1.96*d$se
  return(d)
}
d<- CIs(d)
d$var <- rownames(d)

FillGroup <- function(d){
  
  d$gruppe <- NA
  d$gruppe[d$var %in% Ideology]     <- "Ideology"
  d$gruppe[d$var %in% Profession]   <- "Profession"
  d$gruppe[d$var %in% Age]          <- "Age"
  
  # reorder
  d <- rbind(d[d$var %in% Ideology,],
             d[d$var %in% Profession,],
             d[d$var %in% Age,])
  return(d)
}

d <- FillGroup(d)
d$order <- 1:nrow(d)

GetLabels <- function(d){
  offset <- c("   ")
  
  d$var[d$var %in% Ideology] <- paste(offset,c("Right",
                                               "Left"))
  
  
  d$var[d$var %in% Profession] <- paste(offset,c("Gardener",
                                                 "Teacher",
                                                 "Engineer"))
  
  d$var[d$var %in% Age] <-  paste(offset,c("30",
                                           "40",
                                           "50"))
  
  return(d)
}

d <- GetLabels(d)

# bring in sublabels           
d <- d[order(d$order),]
dd <- data.frame(var= c("IDEOLOGY:",
                        " ",
                        "PROFESSION:",
                        "  ",
                        "AGE:"
),order=c(.5,2.1,2.5,5.1,5.5),
pe=1,se=1,upper=1,lower=1,gruppe=NA)
d <- rbind(d,dd)
d <-d[order(d$order),]
d$var <- factor(d$var,levels=unique(d$var)[length(d$var):1])

yylab  <- c("Change in E[Y]")

f_noideo_likely1_difference = ggplot(d,aes(y=pe,x=var,colour=gruppe)) + geom_hline(yintercept=0, size=0.5, color = "gray", linetype = 1)  + labs(title = "") + coord_flip(ylim = c(-.8, .8)) + geom_pointrange(aes(ymin=lower,ymax=upper),size=1,color="grey20",position = position_dodge(0.9)) + scale_y_continuous(name=yylab,breaks=round(seq(-.8,.8,.2),1),labels=c("-.8","-.6","-.4","-.2","0",".2",".4",".6",".8")) + scale_colour_discrete("Attribute:") + scale_x_discrete(name="") + theme(legend.position="none") + theme(axis.text=element_text(size=24)) + ggtitle("Difference") + theme(plot.title = element_text(size=20, hjust = 0.5)) + annotate("text", x=13.25, y=0.45, label= "", size=4) 
f_noideo_likely1_difference

#############################
# Appendix F
#############################

f1 = grid.arrange(f_left_likely1,f_left_nolikely1,f_left_likely1_difference, ncol = 3, top = textGrob("Panel A: Left-wing respondents",gp=gpar(fontsize=35,fontface = 'bold')))
f2 = grid.arrange(f_right_likely1,f_right_nolikely1,f_right_likely1_difference, ncol = 3,  top = textGrob("Panel B: Right-wing respondents",gp=gpar(fontsize=35,fontface = 'bold')))
f3 = grid.arrange(f_center_likely1,f_center_nolikely1,f_center_likely1_difference, ncol = 3,  top = textGrob("Panel C: Centrist respondents",gp=gpar(fontsize=35,fontface = 'bold')))
f4 = grid.arrange(f_noideo_likely1,f_noideo_nolikely1,f_noideo_likely1_difference, ncol = 3,  top = textGrob("Panel D: Non-identifiers",gp=gpar(fontsize=35,fontface = 'bold')))

pdf('~/Dropbox/Cerrillos 2017/08_replication/f_main_alternative.pdf',width = 20,height=25)
ggarrange(f1,f2,f3,f4, nrow = 4)
dev.off()

